﻿Imports System.Windows.Forms
Imports System.ComponentModel

Public Class LibMskData
    Inherits MaskedTextBox

    Private Const EMPTY_TEXT As String = "  /  /"

    Private _FormatString As String = "dd/MM/yyyy"
    Private _EnterDateNow As Boolean = True
    Private _SelectOnEnter As Boolean = True
    Private _DataValue As Date

    Public Property FormatString() As String
        Get
            Return _FormatString
        End Get
        Set(ByVal value As String)
            _FormatString = value
        End Set
    End Property

    Public Property EnterDateNow() As Boolean
        Get
            Return _EnterDateNow
        End Get
        Set(ByVal value As Boolean)
            _EnterDateNow = value
        End Set
    End Property

    Public Property SelectOnEnter() As Boolean
        Get
            Return _SelectOnEnter
        End Get
        Set(ByVal value As Boolean)
            _SelectOnEnter = value
        End Set
    End Property

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub InitializeComponent()
        Me.SuspendLayout()
        '
        'ACEDateBox
        '
        Me.InsertKeyMode = System.Windows.Forms.InsertKeyMode.Overwrite
        Me.ResumeLayout(False)

    End Sub

    Protected Overrides Sub OnBindingContextChanged(ByVal e As System.EventArgs)
        For Each bind As Binding In Me.DataBindings
            bind.FormatString = Me.FormatString
            bind.FormattingEnabled = True
        Next
        MyBase.OnBindingContextChanged(e)
    End Sub

    Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
        If EnterDateNow And (Me._DataValue = Date.MinValue) Then
            Me.Text = Date.Now.ToString(Me.FormatString)
        End If
        MyBase.OnEnter(e)
    End Sub

    Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
        If Me.Text = EMPTY_TEXT Then
            Me._DataValue = Date.MinValue
        End If
        MyBase.OnLeave(e)
    End Sub

    Private Function IsDateEmtpy() As Boolean
        Dim bSucess As Boolean
        Dim data As Date
        bSucess = Date.TryParse(Me.Text, data)
        Return bSucess
    End Function

    Public Overrides Property Text() As String
        Get
            If Not Date.TryParse(MyBase.Text, _DataValue) Then
                _DataValue = Date.MinValue
            End If

            If _DataValue = Date.MinValue Then
                Return EMPTY_TEXT '_DataValue.ToString(Me.FormatString)
            Else
                Return MyBase.Text
            End If
            'Return MyBase.Text
        End Get
        Set(ByVal value As String)
            If Not Date.TryParse(value, _DataValue) Then
                _DataValue = Date.MinValue
            End If

            If _DataValue = Date.MinValue Then
                MyBase.Text = EMPTY_TEXT
            Else
                MyBase.Text = value
            End If
        End Set
    End Property
End Class
